package com.motionlog.activity;

import android.content.Intent;
import android.os.Bundle;

import com.motionlog.R;
import com.motionlog.view.BottomTabActivity;
import com.motionlog.view.BottomTabHost;
import com.motionlog.view.BottomTabHost.OnTabChangeListener;
import com.motionlog.view.BottomTabWidget;

public class Home extends BottomTabActivity {

	protected static final String TAG = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_home);

		getLocalActivityManager();

		BottomTabHost tabHost = getTabHost();
		BottomTabHost.TabSpec spec;
		Intent intent;

		intent = new Intent(this, NewItem.class);
		spec = tabHost.newTabSpec("newitem").setIndicator("")
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, MotionOrganize.class);
		spec = tabHost.newTabSpec("organize").setIndicator("")
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, MotionStatistic.class);
		spec = tabHost.newTabSpec("statistic").setIndicator("")
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, MoreModule.class);
		spec = tabHost.newTabSpec("more").setIndicator("").setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, Settings.class);
		spec = tabHost.newTabSpec("settings").setIndicator("")
				.setContent(intent);
		tabHost.addTab(spec);

		final BottomTabWidget widget = (BottomTabWidget) findViewById(R.id.tabs);

		int index = getIntent().getIntExtra("tab-index", 0);
		tabHost.setCurrentTab(index);
		String tabId = tabHost.getCurrentTabTag();
		updateTabWidget(tabId, widget);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				android.util.Log.d(TAG, "tabId: " + tabId);
				updateTabWidget(tabId, widget);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		android.util.Log.d(TAG, "home activityResult");
	}

	private void updateTabWidget(String tabId, BottomTabWidget widget) {
		if (tabId.equals("newitem")) {
			widget.setBackgroundResource(R.drawable.btnbar_new);
		} else if (tabId.equals("organize")) {
			widget.setBackgroundResource(R.drawable.btnbar_history);
		} else if (tabId.equals("statistic")) {
			widget.setBackgroundResource(R.drawable.btnbar_calc);
		} else if (tabId.equals("more")) {
			widget.setBackgroundResource(R.drawable.btnbar_more);
		} else if (tabId.equals("settings")) {
			widget.setBackgroundResource(R.drawable.btnbar_set);
		}
	}
}
